/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.editors;

import com.eng.scc.core.data.Address;
import com.eng.scc.core.data.Driver;
import com.eng.scc.core.data.People;
import com.eng.scc.core.db.manager.service.FuelControllerManagerService;
import com.eng.scc.core.util.Error;
import com.eng.scc.core.view.ApplicationContext;
import java.awt.Component;
import java.awt.Dimension;
import java.sql.Date;
import com.eng.scc.core.parts.FormattedTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import org.jdesktop.swingx.JXDatePicker;

/**
 *
 * @author Rafael
 */
public class DriverEditor extends AbstractPanelEditor<Driver>
{
//    public static void main( String[] args )
//    {
//        DriverEditor.edit( null, null );
//    }
    
    public static boolean edit( Component parent, Driver d ) throws Exception
    {
        DriverEditor ce = new DriverEditor();

        ce.setSource( d );
        
        People p = new People();
        Address address = new Address();
        
        if ( d.getPeopleId() > 0 )
        {
            p = FuelControllerManagerService.getInstance().getPeople( d.getPeopleId() );
            
            address = FuelControllerManagerService.getInstance().getAddress( p.getAddressId() );
        }
        
        ce.setPeople( p, address );
        
        ce.populateFields();
        
        boolean acpt = DefaultEditor.createEditor( parent, 
                                           "Editor de Motorista", 
                                           "Formulário de preenchimento de dados do Motorista", 
                                           new Dimension( 700, 700 ), ce );
        
        if ( acpt )
        {
            //da´pra fazer a persistência aqui
            address = ce.getAddress();
            
            if ( address.getId() == 0 )
            {
                FuelControllerManagerService.getInstance().addAddress( address );
            }
            
            else
            {
                FuelControllerManagerService.getInstance().updateAddress( address );
            }
            
            
            p = ce.getPeople();
            p.setAddressId( address.getId() );
            
            if ( p.getId() == 0 )
            {
                FuelControllerManagerService.getInstance().addPeople( p );
            }
            
            else
            {
                FuelControllerManagerService.getInstance().updatePeople( p );
            }
            
            d.setPeopleId( p.getId() );
            
            if ( d.getId() == 0 )
            {
                FuelControllerManagerService.getInstance().addDriver( d );
            }
            
            else
            {
                FuelControllerManagerService.getInstance().updateDriver( d );
            }
        }
        
        return acpt;
    }
    
    public DriverEditor()
    {
        initComponents();
        initMasks();
    }

    private void setPeople( People p, Address a ) 
    {   
        this.userPane.setSource( p, a );
    }
    
    public People getPeople()
    {
        return this.userPane.getPeople();
    }
    
    public Address getAddress()
    {
        return this.userPane.getAddress();
    }
    
    
    private void initMasks()
    {
        try
        {
            tfNumberCNH.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "###########" ) ) );
            
//            tfCNHcategory.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "UUUU" ) ) );
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    @Override
    public void setSource( Driver source )
    {
        if ( source == null )
        {
            throw new IllegalArgumentException( "Client cannot be null!" );
        }

        this.source = source;
    }
    
    private void populateFields()
    {
        try
        {
//            userPane.setPeople( new People() );
//            
            if ( source.getId() > 0 )
            {
//                userPane.setPeople( FuelControllerManagerService.getInstance().getPeople( source.getPeopleId() ) );
                companySelector.setSource( FuelControllerManagerService.getInstance().getCompany( source.getCompanyId() ) );
            }
            
            dtDateDUECNH.setDate( source.getCnhDue() );
            defaultCommission.setValue( source.getDefaultComission() );
            tfCNHcategory.setText( source.getCnhCategory() );
            tfNumberCNH.setText( source.getCnh() );
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    @Override
    public void getSource()
    {
        try
        {
            if ( source != null )
            {
                this.source.setCompanyId( companySelector.getSource().getId() );
                this.source.setCnh( tfNumberCNH.getText() );
                this.source.setCnhCategory( tfCNHcategory.getText() );
                this.source.setCnhDue( dtDateDUECNH.getDate() != null ? new Date( dtDateDUECNH.getDate().getTime() ) : null );
                this.source.setDefaultComission( defaultCommission.getValue() );
            }
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }

    @Override
    public void validateEditor( Error error )
    {        
        if ( companySelector.getSource() == null )
        {
            error.add( "Informe a Empresa que o Motorista está vinculado" );
        }
        
        if ( tfNumberCNH.getText().trim().isEmpty() )
        {
            error.add( "Informe a Número da Carteira de Habilitação" );
        }
        
        if ( tfCNHcategory.getText().trim().isEmpty() )
        {
            error.add( "Informe a Categoria da Carteira de Habilitação" );
        }
        
        userPane.validateForm( error );
    }
    
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        userPane = new com.eng.scc.core.panes.UserPane();
        companySelector = new com.eng.scc.core.parts.CompanySelector();
        lbCompany = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        defaultCommission = new com.eng.scc.core.parts.DecimalTextField();
        jLabel2 = new javax.swing.JLabel();
        panelCNH = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfNumberCNH = new FormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        tfCNHcategory = new com.eng.scc.core.parts.FormattedTextField();

        setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(userPane, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(companySelector, gridBagConstraints);
        
        companySelector.setEditable( false );
        
        lbCompany.setText("Empresa:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lbCompany, gridBagConstraints);

        jLabel1.setText("Comissão Padrão:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel1, gridBagConstraints);

        defaultCommission.setMinimumSize(new java.awt.Dimension(60, 20));
        defaultCommission.setPreferredSize(new java.awt.Dimension(60, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(defaultCommission, gridBagConstraints);

        jLabel2.setText("%");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel2, gridBagConstraints);

        panelCNH.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Carteira de Habilitação", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 204))); // NOI18N
        panelCNH.setLayout(new java.awt.GridBagLayout());

        jLabel3.setText("Número:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 25, 5, 5);
        panelCNH.add(jLabel3, gridBagConstraints);

        jLabel4.setText("Data de Vencimento:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelCNH.add(jLabel4, gridBagConstraints);

        tfNumberCNH.setMinimumSize(new java.awt.Dimension(120, 20));
        tfNumberCNH.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelCNH.add(tfNumberCNH, gridBagConstraints);
        
        
        dtDateDUECNH.setMinimumSize(new java.awt.Dimension(140, 20));
        dtDateDUECNH.setPreferredSize(new java.awt.Dimension(140, 20));
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.NONE;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelCNH.add(dtDateDUECNH, gridBagConstraints);

        jLabel5.setText("Categoria:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 25, 5, 5);
        panelCNH.add(jLabel5, gridBagConstraints);

        tfCNHcategory.setMinimumSize(new java.awt.Dimension(80, 20));
        tfCNHcategory.setPreferredSize(new java.awt.Dimension(80, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelCNH.add(tfCNHcategory, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 163;
        gridBagConstraints.insets = new java.awt.Insets(11, 10, 11, 10);
        add(panelCNH, gridBagConstraints);
    }
    
    private JXDatePicker dtDateDUECNH = new JXDatePicker();
    private com.eng.scc.core.parts.CompanySelector companySelector;
    private com.eng.scc.core.parts.DecimalTextField defaultCommission;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lbCompany;
    private javax.swing.JPanel panelCNH;
    private com.eng.scc.core.parts.FormattedTextField tfCNHcategory;
    private FormattedTextField tfNumberCNH;
    private com.eng.scc.core.panes.UserPane userPane;
}